import 'dart:ui' as ui;
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';

class Utils {
  Future<ui.Image> loadImageFromAssets(ByteData byteData) async {
    Uint8List byteList = byteData.buffer.asUint8List(); // 将ByteData转换为Uint8List
    ui.Codec codec =
        await ui.instantiateImageCodec(byteList); // 使用Uint8List创建图像编解码器
    ui.FrameInfo frameInfo = await codec.getNextFrame(); // 获取图像的帧信息
    ui.Image image = frameInfo.image; // 获取图像对象
    return image;
  }

  Future<String> getImgByGallery(BuildContext context) async {
    final file = await ImagePicker()
        .pickImage(source: ImageSource.gallery, imageQuality: 50);
    if (file == null) return "";
    return file.path;
  }

  Future<String> getImgByCamera(BuildContext context) async {
    final file = await ImagePicker()
        .pickImage(source: ImageSource.camera, imageQuality: 50);
    if (file == null) return "";
    return file.path;
  }
}
